Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var _eventEmitter = _interopRequireDefault(require("./event-emitter")), _apiCode = _interopRequireDefault(require("./api-code")), _socket = _interopRequireDefault(require("./socket")), _api = _interopRequireDefault(require("./api"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var defaults = {
    app: "card",
    appid: ""
}, _default = {
    app: defaults.app,
    appid: defaults.appid,
    initial: function(e) {
        this.app = e.app || defaults.app, this.appid = e.appid || defaults.appid, this.loadUnreadsNum(), 
        _socket.default.on(_socket.default.EVENT_TYPES.MESSAGE_CHAT, this.loadMsg), _socket.default.on(_socket.default.EVENT_TYPES.MESSAGE_CHAT_READ, this.emitChatReadStatus);
    },
    loadMsg: function(e) {
        var t = e.params;
        if (!(+t.issystemchat || t.content && 100 <= t.content.type)) {
            if (!+t.issystem) {
                var a = getApp().globalData, i = (+a.radarCount || 0) + 1;
                a.radarCount = i, _eventEmitter.default.emit("radar.unreads.change", {
                    count: i
                });
            }
            _eventEmitter.default.emit("radar.message.socket", t);
        }
    },
    emitChatReadStatus: function(e) {
        var t = e.params;
        _eventEmitter.default.emit("radar.message.read", t.chatid);
    },
    loadUnreadsNum: function() {
        _api.default.chat.message.unreads().then(function(e) {
            var t = e.data.count;
            getApp().globalData.radarCount = t, _eventEmitter.default.emit("radar.unreads.change", {
                count: t
            });
        });
    },
    checkExistCorp: function(o, e) {
        var n = this;
        return _api.default.market.corp.existmember(e).then(function(e) {
            var t = e.data, a = t.isexist, i = t.issubscribe, r = t.mobile;
            return o && o.guid && (a || n.marketJoin(o)), {
                isexist: a,
                issubscribe: i,
                mobile: r
            };
        });
    },
    marketJoin: function(e) {
        var t = this;
        return _api.default.market.corp.join({
            data: {
                cardid: e.cardid
            }
        }).then(function(e) {
            return e.code !== _apiCode.default.PARAM_MISSING || (t.verifyMobile(), !1);
        });
    },
    verifyMobile: function() {
        wx.showModal({
            content: "请完成名片手机号验证",
            confirmText: "去认证",
            success: function(e) {
                e.confirm && wx.navigateTo({
                    url: "/pages/card/changemobile/changemobile"
                });
            }
        });
    },
    checkNeedReplaceMobile: function(e, t, a, i) {
        a || !t || t && +i == +e.mobile || this.replaceMobileModel(e, i);
    },
    replaceMobileModel: function(t, e) {
        var a = this;
        wx.showModal({
            title: "你已加入商务雷达",
            content: "将关注手机更新为: ".concat(t.mobile, "\n或继续使用原手机: ").concat(e),
            confirmText: "更新手机",
            cancelText: "不更换",
            success: function(e) {
                return a.replaceMobile(t, e.confirm);
            }
        });
    },
    replaceMobile: function(e, t) {
        _api.default.market.corp.replacemobile({
            data: {
                cardid: e.cardid,
                op: t ? "replace" : "notreplace"
            }
        }).then(function() {
            wx.showToast({
                title: "提交成功",
                duration: 500
            });
        });
    }
};

exports.default = _default;